<template>
  <div class="login-container">
    <!-- <div class="login-sidebar"></div> -->
    <el-form
      class="login-form"
      autocomplete="on"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
    >
      <!-- <div class="login-logo">
        <img src="../../assets/images/pro_logo_bw.png" alt>
      </div> -->
      <!-- <h3 class="title">油烟在线监测平台</h3> -->
      <h3 class="title">餐饮业在线大数据监控平台</h3>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          name="username"
          type="text"
          @keyup.enter.native="handleLogin"
          v-model="loginForm.username"
          autocomplete="on"
          placeholder="用户名"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input
          name="password"
          :type="pwdType"
          @keyup.enter.native="handleLogin"
          v-model="loginForm.password"
          autocomplete="on"
          placeholder="密码"
        ></el-input>
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye"/>
        </span>
      </el-form-item>
      <!-- <el-form-item prop="verifyCode" class="my-verify-code">
        <span class="svg-container">
          <svg-icon icon-class="yanzhengma"></svg-icon>
        </span>
        <el-input name="verifyCode" type="text" @keyup.enter.native="handleLogin" v-model="loginForm.verifyCode" autoComplete="on" placeholder="验证码" maxlength="4" />
        <img :src="verifyCodeImg" class="verify-code" @click="refreshVerifyCode">
      </el-form-item>-->
      <el-form-item class="el-form-item_submit">
        <el-button type="primary" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
// import { getVerifyCode } from '@/api/login'

export default {
  name: 'login',
  data() {
    // 校验用户名
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    // 校验密码
    const validatePass = (rule, value, callback) => {
      // if (value.length < 5) {
      //   callback(new Error('密码不能小于5位'))
      // } else {
      callback()
      // }
    }
    return {
      // verifyCodeImg: '', // 验证码图片
      loginForm: {
        username: '',
        password: ''
        // ,
        // verifyCode: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
        // ,
        // verifyCode: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      loading: false,
      pwdType: 'password'
    }
  },
  // watch: {
  //   $route(to, from) {
  //     if (!!to.query.redirect && to.query.redirect === '401') {
  //       this.verifyCodeImg = getVerifyCode()
  //       this.loginForm.verifyCode = ''
  //     }
  //   }
  // },
  created() {
    // this.refreshVerifyCode()
  },
  methods: {
    // // 刷新验证码
    // refreshVerifyCode() {
    //   this.verifyCodeImg = getVerifyCode()
    // },
    // 切换密码显示方式
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    // 登陆操作
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.loading = false
              this.$router.push({ path: '/' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #fff;
$light_gray: #363636;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 44px;
    width: 85%;
    input {
      background: #fff;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 44px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #363636 !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid #d2d8dc;
    background: #fff;
    border-radius: 25px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #f00;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url(../../assets/images/login-bg1.jpg) center center no-repeat;
  background-size: cover;
  .login-sidebar {
    width: 300px;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background: url("../../assets/images/login_bg_h1080.jpg") center bottom;
  }
  .login-logo {
    text-align: center;
    padding-bottom: 20px;
  }
  .login-form {
    // position: absolute;
    // left: 300px;
    // right: 0;
    width: 400px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
    background-color: #fff;
  }
  .svg-container {
    padding: 2px 5px 2px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    margin-top: -3px;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 22px;
    // font-weight: normal;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    // color: #363636;
    color: #3a88ea;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .my-verify-code {
    .el-input {
      width: 260px;
    }
    .verify-code {
      width: 150px;
      height: 44px;
      position: absolute;
      right: 0;
      top: 0;
      cursor: pointer;
      border-radius: 0 25px 25px 0;
    }
  }
  .el-form-item_submit {
    border: none;
    .el-button--primary {
      border: none;
      border-radius: 25px;
      width: 100%;
      // margin-left:15%;
      padding: 13px 20px;
      // background-color: #34caea;
      // background: -moz-linear-gradient(left, #2d97e7 0%, #2eb8d3 100%);
      // background: -webkit-gradient(
      //   linear,
      //   left top,
      //   left bottom,
      //   color-stop(0%, #2d97e7),
      //   color-stop(100%, #2eb8d3)
      // );
      // background: -webkit-linear-gradient(left, #2d97e7 0%, #2eb8d3 100%);
      // background: -o-linear-gradient(left, #2d97e7 0%, #2eb8d3 100%);
      // background: -ms-linear-gradient(left, #2d97e7 0%, #2eb8d3 100%);
      // background: linear-gradient(to right, #2d97e7 0%, #2eb8d3 100%);
      background-color: #3a88ea;
      background: -moz-linear-gradient(left, #3a88ea 0%, #589df4 100%);
      background: -webkit-gradient(
        linear,
        left top,
        left bottom,
        color-stop(0%, #3a88ea),
        color-stop(100%, #589df4)
      );
      background: -webkit-linear-gradient(left, #3a88ea 0%, #589df4 100%);
      background: -o-linear-gradient(left, #3a88ea 0%, #589df4 100%);
      background: -ms-linear-gradient(left, #3a88ea 0%, #589df4 100%);
      background: linear-gradient(to right, #3a88ea 0%, #589df4 100%);
    }
  }
}
</style>
